package krt.wid.gslzs.util;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import krt.wid.gslzs.R;
import krt.wid.gslzs.adpater.AppealAdpater;
import krt.wid.gslzs.adpater.GridViewAdapter;
import krt.wid.gslzs.bean.AppealBean;
import krt.wid.gslzs.bean.Choose_BM;
import krt.wid.util.MToast;


/**
 * Created by Administrator on 2017-08-07.
 */

public class AppealDialog extends Dialog implements View.OnClickListener {

    private Context mContext;
    private String content;
    private OnCloseListener listener;

    TextView choose_subit;


    private List<AppealBean.DataBean> dataList =new ArrayList<>();
    private AppealAdpater adapter;
    private RecyclerView mRecyclerView;
    private  String code="";
    private  String name="";

    public AppealDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }


    public AppealDialog(Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }


    public AppealDialog(Context context, int themeResId, String content, List<AppealBean.DataBean> list, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
        this.dataList=list;

    }

    protected AppealDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.appeal_dialog);
        setCanceledOnTouchOutside(true);
        initView();
        choose_subit=findViewById(R.id.choose_subit);
        choose_subit.setOnClickListener(this);

    }


    private void initView() {
        mRecyclerView=findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter=new AppealAdpater(dataList);
        mRecyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                AppealBean.DataBean data= (AppealBean.DataBean) adapter.getData().get(position);
                name=data.getName();
                code =data.getCode();

                for (int i=0 ;i<dataList.size();i++){
                    if (position !=i){
                        dataList.get(i).setCheck(false);
                    }else {
                        dataList.get(i).setCheck(true);
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });

    }


        @Override
        public void onClick (View v){
            switch (v.getId()) {
                case R.id.choose_subit:
                    if (listener !=null){
                        listener.onClick(this,true,name,code);
                    }
                    break;
                default:
            }
        }

        public interface OnCloseListener {
            void onClick(Dialog dialog, boolean confirm,String name, String code);
        }
    }

